//
//  RebateTopVM.swift
//  kxf
//
//  Created by Mac on 2024/3/11.
//

import Foundation
import UIKit

class RebateTopVM: UIView {
    
    let selfHeight = kFitWidth(171)
    
    var withDrawBlock:(()->())?

    override init(frame:CGRect){
        super.init(frame: CGRect.init(x: kFitWidth(1), y: frame.origin.y, width: SCREEN_WIDHT-kFitWidth(2), height: selfHeight))
        self.backgroundColor = .white
        self.isUserInteractionEnabled = true
        self.layer.cornerRadius = kFitWidth(8)
        self.clipsToBounds = true
        
        initUI()
    }
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    lazy var withdrawBalanceLabel : UILabel = {
        let lab = UILabel()
        lab.textColor = .THEME
        lab.font = .systemFont(ofSize: 24, weight: .medium)
        lab.text = "0.00"
        lab.adjustsFontSizeToFitWidth = true
        
        return lab
    }()
    lazy var withdrawBtn : UIButton = {
        let btn = UIButton()
        btn.backgroundColor = .THEME
        btn.setTitle("提现", for: .normal)
        btn.setTitleColor(.white, for: .normal)
        btn.titleLabel?.font = .systemFont(ofSize: 14, weight: .regular)
        btn.layer.cornerRadius = kFitWidth(8)
        btn.clipsToBounds = true
        
        btn.addTarget(self, action: #selector(withDrawAction), for: .touchUpInside)
        
        return btn
    }()
    lazy var bgImgView : UIImageView = {
        let img = UIImageView()
        img.setImgLocal(imgName: "rebate_img_bg")
        img.isUserInteractionEnabled = true
        
        return img
    }()
    lazy var withdrawBalanceLab : UILabel = {
        let lab = UILabel()
        lab.textColor = .black
        lab.font = .systemFont(ofSize: 14, weight: .medium)
        lab.text = "可提现（元）"
        
        return lab
    }()
    lazy var balanceLabel : UILabel = {
        let lab = UILabel()
        lab.textColor = .black
        lab.font = .systemFont(ofSize: 20, weight: .medium)
        lab.text = "0"
        lab.textAlignment = .center
        lab.adjustsFontSizeToFitWidth = true
//        lab.backgroundColor = WHColor_ARC()
        
        return lab
    }()
    lazy var balanceLab : UILabel = {
        let lab = UILabel()
        lab.textColor = .COLOR_TEXT_BLACK666
        lab.font = .systemFont(ofSize: 12, weight: .regular)
        lab.text = "账户余额（元）"
        lab.textAlignment = .center
        lab.adjustsFontSizeToFitWidth = true
//        lab.backgroundColor = WHColor_ARC()
        
        return lab
    }()
    lazy var lineView : UIView = {
        let vi = UIView()
        vi.backgroundColor = .black
        vi.isHidden = true
        
        return vi
    }()
    lazy var settleLabel : UILabel = {
        let lab = UILabel()
        lab.textColor = .black
        lab.font = .systemFont(ofSize: 20, weight: .medium)
        lab.text = "0"
        lab.textAlignment = .center
        lab.adjustsFontSizeToFitWidth = true
//        lab.backgroundColor = WHColor_ARC()
        
        return lab
    }()
    lazy var settleLab : UILabel = {
        let lab = UILabel()
        lab.textColor = .COLOR_TEXT_BLACK666
        lab.font = .systemFont(ofSize: 12, weight: .regular)
        lab.text = "待结算（元）"
        lab.textAlignment = .center
        lab.adjustsFontSizeToFitWidth = true
//        lab.backgroundColor = WHColor_ARC()
        
        return lab
    }()
}

extension RebateTopVM{
    @objc func withDrawAction() {
        if self.withDrawBlock != nil{
            self.withDrawBlock!()
        }
    }
    func updateUI(dict:NSDictionary)  {
        withdrawBalanceLabel.text = "\(dict["ketiamount"]as? Double ?? 0.00)"
        balanceLabel.text = "\(dict["totalReamin"]as? Double ?? 0.00)"
        settleLabel.text = "\(dict["actAmount"]as? Double ?? 0.00)"
    }
}

extension RebateTopVM{
    func initUI() {
//        addSubview(withdrawBalanceLabel)
        addSubview(withdrawBtn)
        
        addSubview(bgImgView)
//        bgImgView.addSubview(withdrawBalanceLab)
        bgImgView.addSubview(balanceLabel)
        bgImgView.addSubview(balanceLab)
        bgImgView.addSubview(lineView)
//        bgImgView.addSubview(settleLabel)
//        bgImgView.addSubview(settleLab)
        
        setConstrait()
    }
    func setConstrait() {
//        withdrawBalanceLabel.snp.makeConstraints { make in
//            make.left.equalTo(kFitWidth(31))
//            make.top.equalTo(kFitWidth(32))
//            make.width.equalTo(kFitWidth(220))
//        }
        withdrawBtn.snp.makeConstraints { make in
            make.right.equalTo(kFitWidth(-24))
            make.top.equalTo(kFitWidth(26))
            make.width.equalTo(kFitWidth(78))
            make.height.equalTo(kFitWidth(30))
        }
        bgImgView.snp.makeConstraints { make in
            make.centerX.lessThanOrEqualToSuperview()
            make.width.equalTo(kFitWidth(347))
            make.height.equalTo(kFitWidth(95))
            make.bottom.equalTo(kFitWidth(-15))
        }
//        withdrawBalanceLab.snp.makeConstraints { make in
//            make.left.equalTo(kFitWidth(18))
//            make.top.equalToSuperview()
//        }
        lineView.snp.makeConstraints { make in
            make.left.equalTo(kFitWidth(132))
            make.top.equalTo(kFitWidth(32))
            make.width.equalTo(kFitWidth(1))
            make.height.equalTo(kFitWidth(17))
        }
        balanceLabel.snp.makeConstraints { make in
            make.left.equalTo(kFitWidth(5))
            make.right.equalTo(lineView.snp.left).offset(kFitWidth(-5))
            make.top.equalTo(kFitWidth(22))
        }
        balanceLab.snp.makeConstraints { make in
            make.centerX.lessThanOrEqualTo(balanceLabel)
            make.top.equalTo(kFitWidth(52))
        }
//        settleLabel.snp.makeConstraints { make in
//            make.left.equalTo(lineView.snp.right).offset(kFitWidth(5))
//            make.top.equalTo(balanceLabel)
//            make.width.equalTo(kFitWidth(120))
//        }
//        settleLab.snp.makeConstraints { make in
//            make.centerX.lessThanOrEqualTo(settleLabel)
//            make.centerY.lessThanOrEqualTo(balanceLab)
//        }
    }
}
